package TUILayer;
import java.util.Scanner;
import ControlLayer.*;

/**
 * User interface to Loan controller. 
 *
 * @author PET
 * @verson 1.0 (04-11-2009)
 */
public class LoanUI
{
    private LoanCtr loanCtr;
    
    /**
     * Constructs a LoanUI with two controlllayers.
     * 
     * @param addressCtr. Our AddressController.
     * @param dvdCtr. Our DvdControll.
     */
    public LoanUI()
    {
        loanCtr = new LoanCtr();
    }
    
    /**
     * Starts the menu loop.
     */
    public void start()
    {
        dvdMenu();
    }
    
    /**
     * Menu loop.
     */
    public void dvdMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choise = writeLoanMenu();
            if(choise == 1)
            {
                loanDvd();
            } // IF1 END
            else
            {
                if(choise == 2)
                {
                    returnDvd();
                } //if2 end
                if(choise == 3)
                {
                    printLoans();
                }//if3 end
                if ( choise==4)
                {
                    editLoan();
                }//if4 end
                else
                {exit=true;
                }
            } 
        } 
    }

    /**
     * prints out the menu.
     * 
     * @return choise The choosen menu.
     */
    private int writeLoanMenu()
    {
        int choise = 0;
            try
            {
                Scanner keyboard = new Scanner(System.in);
                System.out.println("\f *** Loan Menu ***");
                System.out.println(" (1) Create a loan.");
                System.out.println(" (2) Turn back loan.");
                System.out.println(" (3) PrintLoans.");
                System.out.println(" (4) Edit loan by ID.");
                System.out.println(" (5) Return.");
                System.out.print("\n Make your choise: ");
                choise = keyboard.nextInt();
            }
            catch(Exception e)
            {
            }

            return choise;
    }
    
    /**
     * This is used to stall the menu so we can print.
     */
    public void inputContinue()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Press Enter to continue:  ");
         String code = keyboard.nextLine();
    }
            
    /**
     * KeyBoard Scanner - loan id
     * 
     * @return id. Int from the keyboard.
     */         
    public int inputId()
    {
        boolean ok = false;
        int id = 0;
        while(!ok)
        {  
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Write Id:  ");
            try
            {
                id = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You should insert an Integer");
                String input = keyboard.nextLine();
            }
        }//end while
        return id;
    }
    
    /**
     * KeyBoard Scanner - loan serie nr 
     * 
     * @return code. String from the keyboard.
     */
    public String inputSerialNumber()
    {
         Scanner keyboard = new Scanner(System.in);
         System.out.println(" Write Serialnumber:  ");
         String code = keyboard.nextLine();
         return code;           
    }
       
    /**
     * KeyBoard Scanner - loan Date
     * 
     * @return code. String from the keyboard.
     */
    public String inputDate()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println("Type Loan Date:  ");
         String code = keyboard.nextLine();
         return code;
    } 
    
    
    /**
     * KeyBoard Scanner -loan period
     * 
     * @return code. Int from the keyboard.
     */
    public int inputPeriod()
    {
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write how many days you loan the dvd:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("Write an integer");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
    
    /**
     * KeyBoard Scanner - Person Phone
     * 
     * @return code. String from the keyboard.
     */
    public String inputPersonName()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println("Type in name of Loaner: ");
         String code = keyboard.nextLine();
         return code;
    }
     public String inputPersonID()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println("Type in ID of Loaner: ");
         String code = keyboard.nextLine();
         return code;
    }
    
    /**
     * KeyBoard Scanner - DVDTitle
     * 
     * @return code. String from the keyboard.
     */
    public String inputDvdTitle()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println("Type title of DVD you want to loan:  ");
         String code = keyboard.nextLine();
         return code;
    }
    
    
 
    
    /**
     * KeyBoard Scanner - Return date
     * 
     * @return code. String from the keyboard.
     */
    public int inputPeriodLoaned()
    {
         int code=0;
        Scanner keyboard = new Scanner(System.in);  
         System.out.println("Type after how many days you returned it:  ");
         try{
          code = keyboard.nextInt();
        }
        catch (Exception e)
            {
             System.out.println("Write an integer");
             String input = keyboard.nextLine();
           }
        
         return code;
    }
    
    /**
     * Creates a loan.
     */
   public void loanDvd()
    {
        try
        {
            
            loanCtr.setLoanRegister(inputId(), inputDate(), inputPeriod(),inputDvdTitle() ,inputPersonID());
            inputContinue();
        }
        catch(Exception e)
        {
            System.out.println("You must choose a person and DVD that is already created in the system");
            inputContinue();
        }
        
    }
    
   public void returnDvd()
    {
        try
        {
            System.out.println(loanCtr.returnLoan(inputId(),inputPeriodLoaned()));
            inputContinue();
        }
        catch(Exception e)
        {
            System.out.println("You must choose a valid ID of loan");
            inputContinue();
        }
    }
    
    public void printLoans()
    {
        System.out.println(loanCtr.printLoans());
        inputContinue();
    }
    
    public void editLoan()
    {
        try{
        System.out.println("Input the loan's ID that you want to change");
        loanCtr.editLoan(inputId(), inputDate(), inputPeriod());
        inputContinue();}
        
        catch(Exception e)
        {System.out.println("Loan doesn't exist");
            inputContinue();
        }
    }
   
     
}